using System;
using System.Collections.Generic;
using System.Linq;
namespace TemplateWeb.Controllers
{
	#region using
	using System.Web.Mvc;
	using TemplateWeb.Models.ViewModels;
	using TemplateWeb.Models;
	using TemplateWeb.Models.Repositories;
	using TemplateWeb.Helpers;
	using System.Web.Security;
	#endregion
	public partial class AdminController: BaseController
	{
		public ActionResult SelectDanhGia()
		{
			var data = new DataTransferViewModel
			{
				EnumViewModelType = EnumViewModel.ADMIN_DANHGIA
			};
			return View(CreateViewModel(data));
		}


		public ActionResult SelectDanhGiaByIDSach(int idsach)
		{
			var data = new DataTransferViewModel
			{
				IDSach = idsach,
				EnumViewModelType = EnumViewModel.ADMIN_DANHGIA,
			};
			return View("SelectDanhGia", CreateViewModel(data));
		}

		public ActionResult SelectDanhGiaByIDSachPaging(int idsach)
		{
			var data = new DataTransferViewModel
			{
				IDSach = idsach,
				EnumViewModelType = EnumViewModel.ADMIN_DANHGIA,
			};
			return PartialView("Templates/TH_ListDanhGia", CreateViewModel(data));
		}


		public ActionResult DeleteDanhGia(int id, int page = 1)
		{
			var result = _repDanhGia.Delete(id);
			var data = new DataTransferViewModel
			{
				InfoText = result ? "Item has been deleted" : "Cannot delete item!",
				EnumViewModelType = EnumViewModel.ADMIN_DANHGIA,
				CurrentPage = page
			};
			return View("SelectDanhGia", CreateViewModel(data));
		}

		public ActionResult EditDanhGia(int page, int id)
		{
			var data = new DataTransferViewModel
			{
				EnumViewModelType = EnumViewModel.ADMIN_DANHGIA,
				CurrentPage = page,
				IntID = id
			};
			return PartialView("Templates/TH_EditDanhGia", CreateViewModel(data));
		}

		public ActionResult CancelEditingDanhGia(int page)
		{
			var data = new DataTransferViewModel
			{
				EnumViewModelType = EnumViewModel.ADMIN_DANHGIA,
				CurrentPage = page
			};
			return PartialView("Templates/TH_ListDanhGia", CreateViewModel(data));
		}

		[ValidateInput(false)]
		[AcceptVerbs(HttpVerbs.Post)]
		public ActionResult UpdateDanhGia(FormCollection forms)
		{
			string id = forms.GetValues("DanhGia_ID")[0];
			string idsach = forms.GetValues("DanhGia_IDSach")[0];
			string diem = forms.GetValues("DanhGia_Diem")[0];
			var danhgia = _repDanhGia.SelectByID(int.Parse(id));
			danhgia.IDSach = int.Parse(idsach.Replace(".", "").Replace(",", ""));
			danhgia.Diem = int.Parse(diem.Replace(".", "").Replace(",", ""));
			var result = _repDanhGia.Save();
			var data = new DataTransferViewModel
			{
				EnumViewModelType = EnumViewModel.ADMIN_DANHGIA,
			};
			return View("SelectDanhGia", CreateViewModel(data));
		}


		[ValidateInput(false)]
		[AcceptVerbs(HttpVerbs.Post)]
		public ActionResult InsertDanhGia(FormCollection forms)
		{
			string idsach = forms.GetValues("DanhGia_IDSach")[0];
			string diem = forms.GetValues("DanhGia_Diem")[0];
			var newItem = new DanhGia
			{
				IDSach = int.Parse(idsach),
				Diem = int.Parse(diem)
			};
			var result = _repDanhGia.Insert(newItem);
			var data = new DataTransferViewModel
			{
				IDSach = int.Parse(idsach),
				EnumViewModelType = EnumViewModel.ADMIN_DANHGIA,
				InfoText = result ? "New item has been added" : "Cannot insert new item"
			};
			return View("SelectDanhGia", CreateViewModel(data));
		}


	}
}